import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const CHANGE = 'change'

import {
	getUserInfo
} from '../api/user.js'
import {
	getCateList
} from '../api/home.js'

export default new Vuex.Store({
	state: {
		token: "",
		uid: '',
		userInfo: {},
		isLogin: false,
		cate: []
	},
	getters: {},
	mutations: {
		changea(state, val) {
			state.a = val
		},
		[CHANGE](state, {
			key,
			value
		}) {
			state[key] = value
		},
		clear(state) {
			state.token = "";
			state.uid = '';
			state.userInfo = {};
			state.isLogin = false;
		}
	},
	actions: {
		getUserInfo(json, token) {
			console.log(json);
			getUserInfo({
				token
			}).then(res => {
				console.log(res);
				if (res.code == 0) {
					json.commit('change', {
						key: "getUserInfo",
						value: res.userInfo
					})
					json.commit('change', {
						key: 'uid',
						value: res.uid
					})
					json.commit('change', {
						key: 'token',
						value: res.token
					})
					json.commit('change', {
						key: 'isLogin',
						value: true
					})
				} else {
					this.$toast(res.msg);
				}

			})
		},
		getCate({
			state,
			commit
		}) {
			getCateList().then(res => {
				console.log(res.data.data);
				commit('change', {
					key: "cate",
					value: res.data.data
				})
			})
		}
	},
	modules: {}
})
